using UnityEngine;
using System.Collections;
using System;

public class shieldClass : MonoBehaviour {
	
	public globalVarsClass gv;
	private OTSprite self;
	public OTSprite player;
	public OTSprite tempShield;
	public OTSprite blastWaveShield;
	
	private float shieldAlpha;
	private Color shieldTintColor;
	
	/// <summary>
	/// Gem power variables.
	/// </summary>
	/// 
	/// 
	public float shieldHPUp;                             // Shield power up for: red, red+red
	public float shieldBeforeActivation = 1000.0f;
	
	public float counterAttackRange;                     // Shield power up for: green, green+green
	public int counterAttackDamage;
	public bool counterAttackActive;
	
	public float slowdownLvl;                            // Shield power up for: blue, blue+blue
	public float slowdownTime;
	public bool freezerActive;
	
	public float temporaryShieldCooldownTime;            // Shield power up for: red+green
	public float temporaryShieldHP = 0.0f;
	
	public float invulnerabilityCooldownTime;            // Shield power up for: red+blue
	
	public float blastRange;                             // Shield power up for: blue, blue+blue
	public float blastWaveSpeed = 0.0f;
	public float blastWaveMove = 0.1f;
	
	
	/// <summary>
	/// Use this for initialization.
	/// </summary> 
	void Start () {
		//player = GameObject.Find("Player") as OTSprite;
		gv = GameObject.Find("GlobalVariables").GetComponent<globalVarsClass>();
		gv.shieldHP = 100;
		
		self = this.GetComponent<OTSprite>();
	
		tempShield = GameObject.Find("TemporaryShield").GetComponent<OTSprite>();
		blastWaveShield = GameObject.Find("BlastWaveShield").GetComponent<OTSprite>();
	}
	
	public void RefreshShieldBonusType() {
		// Bonuses deactivation
		
		if (shieldBeforeActivation < gv.shieldHP) {
			gv.shieldHP = shieldBeforeActivation;
			shieldBeforeActivation = 1000.0f;
		}
		shieldHPUp = 0.0f;
		
		//counterAttackRange = -1.0f; 
		//counterAttackDamage = 0.0f;
		counterAttackActive = false;
	
		//slowdownLvl = 1.0f;
		//freezeTime = 0.0f;
		freezerActive = false;
		
		temporaryShieldCooldownTime = 0.0f;
	
		invulnerabilityCooldownTime = 0.0f;
	
		blastRange = 0.0f;
		blastWaveShield.size = new Vector2(0, 0);
		blastWaveSpeed = 0.0f;
		

		// Bonuses activation 		
		switch (gv.shieldState) {
			case 1: {
					shieldHPUp = 25.0f;
					powerUpTheShield();
				}
				break;
			case 2: {
					shieldHPUp = 50.0f;
					powerUpTheShield();
				}
				break;
			case 3: {
					counterAttackRange =  5.0f; 
					counterAttackDamage = 20;
					counterAttackActive = true;
				}
				break;
			case 4: {
					counterAttackRange =  10.0f; 
					counterAttackDamage = 30;
					counterAttackActive = true;
				}
				break;
			case 5: {
					slowdownLvl = 0.5f;  // speed * 0.5
					slowdownTime = 10.0f;
					freezerActive = true;
				}
				break;
			case 6: {
					slowdownLvl = 0.0f;  // speed * 0, frozen
					slowdownTime = 5.0f;
					freezerActive = true;
				}
				break;
			case 7: 
				generateTemporaryShield();
				break;
			case 8: 
				invulnerabilityCooldownTime = 10.0f;
				break;
			case 9: 
				makeBlast();
				break;
		}
	}
	
	public void powerUpTheShield() {
		shieldBeforeActivation = gv.shieldHP;
		gv.shieldHP += shieldHPUp; 
	}
	
	public void generateTemporaryShield() {
		temporaryShieldCooldownTime = 10.0f;
		temporaryShieldHP = 50.0f;
	}
	
	public void makeBlast() {
		blastRange = 20.0f;
	}
	
	/// <summary>
	/// Damage get function.
	/// </summary>
	public void getDamage(float dmgAmount, OTSprite shooter) {
		if (invulnerabilityCooldownTime == 0.0f) {
			
			// get the damage
			if ((temporaryShieldHP > 0.0f) || (temporaryShieldCooldownTime > 0.0f))
				temporaryShieldHP -= dmgAmount;
			else 
				gv.shieldHP -= dmgAmount;
			
			if (counterAttackActive) {
				if ((shooter != null) && (Vector2.Distance(shooter.position, player.position) < counterAttackRange))
					if (shooter.GetComponent<badGuyClass>().takeDamage(counterAttackDamage, 1.0f)) {
						Destroy(shooter.gameObject);
						gv.MobList.Remove(shooter);
						gv.MobCount -= 1;
					}
						
			}
		
			if (freezerActive) {
				//shooter.GetComponent<badGuyClass>().takeDamage(10, slowdownLvl);
			}
			
		}else{
			invulnerabilityCooldownTime -= Time.deltaTime;
			if (invulnerabilityCooldownTime < 0.0f)
				invulnerabilityCooldownTime = 0.0f;
		}
	}
		
	private void recalculateShieldDamage() {
		if (temporaryShieldCooldownTime > 0) {
			temporaryShieldCooldownTime -= Time.deltaTime;
			tempShield.alpha = temporaryShieldHP/50.0f;
		} else {
			tempShield.alpha = 0.0f;
		}
			
		if (gv.shieldHP <= 100.0f) {
			shieldTintColor = new Color(0, 0, 255, 255);
			shieldAlpha = (gv.shieldHP - 66.7f)/33.3f;
			
			if (gv.shieldHP <= 66.6f) {
				shieldTintColor = new Color(0, 255, 0, 255);
				shieldAlpha = (gv.shieldHP - 33.4f)/33.3f;
				
				if (gv.shieldHP <= 33.3f) {
					shieldTintColor = new Color(255, 0, 0, 255);
					shieldAlpha = gv.shieldHP/33.3f;
				}
			}
		}
	}
		
	/// <summary>
	/// Update is called once per frame.
	/// </summary>
	void Update () {
		
		//for testing
		if (gv.shieldHP <= 0)
			gv.shieldHP = 100;
		
		this.recalculateShieldDamage();
		
		self.tintColor = shieldTintColor;
		self.alpha = shieldAlpha + 0.2f;      // ! That's for shild not to become too transparent
		
		if (blastWaveShield.size.x < blastRange) {
			blastWaveShield.size = new Vector2(blastWaveShield.size.x + 0.1f, blastWaveShield.size.y + 0.1f);
			
			if (blastWaveSpeed == 0.0f)
				blastWaveSpeed = blastWaveShield.size.x;
			
			if (blastWaveShield.size.x >= blastRange) {
				blastRange = 0.0f;
				blastWaveShield.size = new Vector2(0, 0);
			}
		}
	}
}
